<?php

/**
 * Created by Green Studio.
 * File: CommonWidget.class.php
 * User: TianShuo
 * Date: 14-1-24
 * Time: 下午2:52
 */

namespace Common\Widget;

use Think\Controller;

/**
 * 侧边栏能过小挂件
 * Class CommonHeaderWidget
 * @package Home\Widget
 */
class SideBarWidget extends Controller {

    /**
     * 小挂件的名字
     * @var type 
     */
    protected $name = '';

    /**
     * 小挂件的描述
     * @var type 
     */
    protected $desc = '';

    /**
     * 挂件的id
     * @var type 
     */
    protected $id = '';

    /**
     * 小工具选项
     * @var type 
     */
    protected $widget_options = array();

    /**
     * 渲染至前端的标题
     * @var type 
     */
    protected $title = '';

    /**
     * 渲染小工具
     * @param type $data
     */
    public function render($data) {
        
    }

    public function getSideBar() {
        $widgets = D('Options')->getByModule('widget');
        foreach ($widgets as $key => $value) {
            $data = unserialize($value['option_value']);
            $widget = $data['widget_file_name'];
            include_once "/SideBar/{$widget}Widget.class.php";
            $class = "\\Common\Widget\\" . $widget . 'Widget';
            $widgetObj = new $class();
            $widgetObj->render($data);
        }
    }

    public function getAll() {
        $widgets = glob(__DIR__ . "/SideBar/*.php");
        $data = array();
        foreach ($widgets as $k => $v) {
            $v = basename($v);
            include_once "/SideBar/{$v}";
            list($name) = explode('.', $v);
            $class = "\\Common\Widget\\" . $name;
            $widgetObj = new $class();
            $data[$widgetObj->id] = array(
                'name' => $widgetObj->name,
                'desc' => $widgetObj->desc,
                'form' => $widgetObj->setting()
            );
        }
        return $data;
    }

    /**
     * 设置小工具
     */
    public function setting() {
        
    }

    public function add() {
        
    }

    public function save($data) {
        empty($data['id']) && $this->error('id不存在');
//        $wid
    }

    public function update() {
        
    }

    /**
     * 
     */
    public function init() {
        
    }

}
